﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PascalConverter
{
    public static class ExtensionMethods
    {
        public static bool ContainsIgnoreCase(this string str, string other)
        {
            return str.ToLower().Contains(other.ToLower());
        }

        public static string ReplaceIgnoreCase(this string str, string replacementStr)
        {
            string copy = str;
            str = str.ToLower().ReplaceFirst(replacementStr.ToLower(), replacementStr);
            int startIndex = str.IndexOf(replacementStr);
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsUpper(copy[i]) && (i < startIndex || i > (startIndex + replacementStr.Length)))
                {
                    str = str.ReplaceAtIndex(str[i].ToString(), copy[i].ToString(), i);
                }
            }
            return str;
        }

        public static string ReplaceIgnoreCaseAndConvertToPascal(this string str, string replacementStr)
        {
            string copy = str;
            str = str.ToLower().ReplaceFirst(replacementStr.ToLower(), replacementStr);
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsUpper(copy[i]))
                {
                    str = str.ReplaceAtIndex(str[i].ToString(), copy[i].ToString(), i);
                }
            }
            return str;
        }

        public static string ReplaceFirst(this string str, string oldStr, string newStr)
        {
            return new Regex(oldStr).Replace(str, newStr, 1);
        }

        public static string ReplaceAtIndex(this string str, string oldStr, string newStr, int index)
        {
            return new Regex(oldStr).Replace(str, newStr, 1, index);
        }

        public static string ConvertToString(this string[] array)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
                builder.Append('/');
            }
            return builder.ToString();
        }

        private static string ToUpper(this string str)
        {
            string newStr = String.Empty;
            newStr += Char.ToUpper(str[0]);
            for (int i = 1; i < str.Length; i++)
            {
                newStr += str[i];
            }
            return newStr;
        }
    }
}
